#include <rtthread.h>
#include "heartbeat.h"
#include "Switch.h"
#include "user_led.h"
#include "gpio.h"
#include "Model_info.h"
#include "iwdg.h"
#include "tim.h"
#include "user_led.h"
#include "adc.h"

void Heartbeat_Send(void);
void Init_Heartbeat(Heartbeat_TCB_t *tcb,uint16_t timeout);
void Set_LED_Link(bool status);
Heartbeat_TCB_t heartbeat_handle;

uint8_t led_link=0;
bool     send_flag=false;
void Enable_LED_Link(void)
{
	led_link=1;
}
void DisEnable_LED_Stop(void)
{
	led_link=0;
}
//uint16_t ad_value=0;
//获取随机ADC值
uint16_t Get_RNG_ADC()
{
	uint16_t aa=0;
	uint16_t adc_value=0;
	for(uint8_t i=0;i<16;i++)
	{
		//HAL_ADC_ConfigChannel(&hadc1, ADC_CHANNEL_10, ADC_SAMPLETIME_19CYCLES_5);
		adc_value=adc_value<<1;
		//adc_value=ad_value;
		HAL_ADC_Start(&hadc1);
	  HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY);
		aa=HAL_ADC_GetValue(&hadc1);
	  adc_value+=(aa&0x0001);
	}
	//ad_value=aa;
	return adc_value;
}
uint32_t Get_RNG()
{
	uint16_t rng_value;
	//return Get_lora_Random();
	//srand(Get_RNG_ADC());
	//rng_value=rand();
}

void LED_Link_Run()
{
	if(led_link)
	{
		HAL_GPIO_WritePin(LED_LINK_GPIO_Port, LED_LINK_Pin, GPIO_PIN_SET);
	}
	else
	{
		HAL_GPIO_TogglePin(LED_LINK_GPIO_Port,LED_LINK_Pin);
	}
}
//定时器回调
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if(htim==&htim16)
	{
		heartbeat_handle.status=false;
	}
}
//复位超时计数值
void ReSetOverTime()
{
	__HAL_TIM_SetCounter(&htim16,0);
	//HAL_TIM_Base_Start_IT(&htim16);
}
void Strat_SendHeart(uint16_t time)
{
	if(!send_flag)
	{
		heartbeat_handle.time=0;
		heartbeat_handle.time_wait=time;
		send_flag=true;
	}
}
uint16_t rng_time=0;
/// @brief 心跳线程入口
/// @param  
void Heartbeat_thread_entry(void *paremeter)
{
	uint16_t heartbeat_time=0;
	Init_Heartbeat(&heartbeat_handle,10000);
	HAL_TIM_Base_Start_IT(&htim16);
	rt_thread_mdelay(500);
	heartbeat_time=500+(model_info_handle.general_info.SN%20)*100;
	//Heartbeat_Send();
	while(1)
	{
		if(heartbeat_handle.recive)
		{
			heartbeat_handle.status=true;
			heartbeat_handle.recive=false;
			Strat_SendHeart(heartbeat_time);
			ReSetOverTime();
		}
		if(send_flag)
		{
			heartbeat_handle.time+=100;
		  if(heartbeat_handle.time>=heartbeat_handle.time_wait)
		  {
				send_flag=false;
				heartbeat_handle.time=0;
			  Heartbeat_Send();
		  }
		}
		if(heartbeat_handle.status==false)
		{
			if(!send_flag)
			{
				//rng_time=(Get_RNG()%10)*200;
				rng_time=(Get_RNG_ADC()%10)*200;
			  Strat_SendHeart(rng_time+500);
			}
		}
		Set_LED_Link(heartbeat_handle.status);
		LED_Link_Run();
		HAL_IWDG_Refresh(&hiwdg);
		rt_thread_mdelay(100);
	}
}
/// @brief 设置 连接指示灯状态
/// @param  
void Set_LED_Link(bool status)
{
	if(status)   //连接正常
	{
		Enable_LED_Link();
	}
	else        //断开连接
	{
		DisEnable_LED_Stop();
	}
}

/// @brief 初始化心跳
/// @param  
void Init_Heartbeat(Heartbeat_TCB_t *tcb,uint16_t timeout)
{
	tcb->recive=false;
	tcb->status=false;
	tcb->time=0;
	tcb->timeout=timeout;
}
/// @brief 接收心跳
/// @param  
void Heartbeat_Receive(void)
{
	heartbeat_handle.recive=true;
	//heartbeat_handle.time_wait=0;
}

/// @brief 发送心跳
/// @param  
void Heartbeat_Send(void)
{
	Switch_Protocol sw;
	sw.id=model_info_handle.general_info.SN;
	sw.status.relay_out1=User_Get_Relay_status().IN1;
	sw.status.relay_out2=User_Get_Relay_status().IN2;
	sw.status.relay_out3=User_Get_Relay_status().IN3;
	sw.status.relay_out4=User_Get_Relay_status().IN4;
	sw.status.sw_in1=User_Get_Sw_status().IN1;
	sw.status.sw_in2=User_Get_Sw_status().IN2;
	sw.status.sw_in3=User_Get_Sw_status().IN3;
	sw.status.sw_in4=User_Get_Sw_status().IN4;
	sw.type=SW_Heartbeat;
	User_LoRaSend_Data((uint8_t *)&sw,sizeof(Switch_Protocol));
}